home *** CD-ROM | disk | FTP | other *** search
- Path: news.bellglobal.com!stupy
- From: stupy@freenet.durham.org (Steve Tupy)
- Newsgroups: comp.lang.c++
- Subject: Re: Q: Template functions with constants instead of types????
- Date: 6 Mar 1996 07:57:14 GMT
- Organization: Durham Free-net
- Message-ID: <4hjggq$k40@news.bellglobal.com>
- References: <313D005B.1E1D@interlinx.qc.ca>
- NNTP-Posting-Host: freenet.durham.org
- X-Newsreader: TIN [version 1.2 PL2]
-
- Mathieu Frenette (midk@interlinx.qc.ca) wrote:
- : Ok, now, let's go with the rough stuff! :) What I want is to be able to
- : define a function using a DEFINEd constant (let's name it "CONSTANT")
- : and then be able to call the function with CONSTANT having arbitrary
- : values. For each value of CONSTANT, the compiler would generate a
- : different function.
-
- : In example, let's suppose we could define something like :
-
- : template <constant T> inline void Func( void )
- : {
- : printf( "%d\n", T );
- : }
-
- No, no, you are missing a serious point of templating. It is used in
- cases where generic data types are used. Here you will run into problems
- when you instantiate a template using doubles or longs as you insist on
- using an integer type for your printf. You should use cout instead of printf
- so that all data types may be supported. Also, you do not use "constant" in
- a template declaration, you use <class T> or <class T,char S> or whatever
- have you, it doesn't matter.
-
- : And then use it like that :
-
- : Func<1>();
- : Func<2>();
- : Func<3>();
-
- : The compiler would generate three similar Func functions with T replaced
- : by the values 1,2,3.
-
- : If you know the syntax for such a template, PLEASE LET ME KNOW!!! :)
-
- : If you're wondering why I'm looking for such a complicated thing, here's
- : why :
-
- Quite the contrary, there is nothing complicated about this. Use
- somthing like this...
-
- template <class T>
- incline void Func(const T) {
- cout << T << endl;
- }
-
- : I have a function that uses a DEFINEd constant (which it absolutely
- : needs for optimization reasons) and I want to include this function in a
-
- Let me get this right. You want to use only a constant value but you
- want to use templates also? This is a contradiction. The whole point of
- templating is that you do not know while writing it what type of data will
- be passed to it, so if in this case, you will ALWAYS know what is passed,
- why template it at all? My above example should be placed into a header and
- NOT compiled into a lib or it won't work properly. THat way, when you are
- coding, you include the header, write your code using the Func call and your
- compiler will instantiate the template.
-
- : library for all my demo group other coders to use. The problem is that,
- : if the function is used 4 times in the program, it'll be with 4
- : different values assigned to the constant...
-
- You really are missing the point behind templates... consider the
- following...
-
- template <class T>
- const T& Max(const T& A,const T& B) {
- return (A >= B?A:B);
- }
- Here you are returning data of type T by comparing two other data
- types of T. If these were longs, ints, doubles, floats , you would achieve
- the desired results. If you were to then code something like this, you would
- get an error....
-
- double a = Max(const int a, const float b);
-
- Big time error, the data types have to be the same as each other.
- The "T" in the above case is all in the same scope and is consider to all be
- one type of data that is the same.
-
- : If you have any idea else than the "hypothetical" template <const T>,
- : please let me know...
-
- Well, this should help you get started. I hope you buy a book on
- this, it sounds like you have a ways to go yet with templates.
-
- Take care!
-
- --
- Steve
-